﻿using HCMS.Const;
using HCMS.Entities.Manage;
using HCMS.Extensions;
using HCMS.IServices.Manage;
using HCMS.Web.Manage.Filter;
using HCMS.Web.Manage.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Volo.Abp.AspNetCore.Mvc;

namespace HCMS.Web.Manage.Controllers
{
    /// <summary>
    /// 管理系统 基础控制器
    /// </summary>
    [Authorize] // [Authorize]仅允许经过身份认证的用户访问控制器
    [TypeFilter(typeof(ActionAuthFilter))]
    [TypeFilter(typeof(ActionExecutionLogsFilter))]
    [TypeFilter(typeof(ResultExecutionLogsFilter))]
    public class BaseController : AbpController
    {
        /// <summary>
        /// 当前登录 用户信息
        /// </summary>
        public CurrentAdminModel CurrentAdmin
        {
            get
            {
                var result = new CurrentAdminModel()
                {
                    AdminId = User.FindFirstValue(ManageConst_ClaimTypes.Id).ToInt(0),
                    AdminName = User.FindFirstValue(ManageConst_ClaimTypes.Name) ?? "",
                    RoleId = User.FindFirstValue(ManageConst_ClaimTypes.RoleId).ToInt(0)
                };

                var adminAppService = Request.HttpContext.RequestServices.GetRequiredService<IAdminAppService>();
                result.Admin = adminAppService.BaseFirstOrDefaultAsync(t => t.Id == result.AdminId).Result ?? new Admin();

                return result;
            }
        }
    }
}
